<?php
    /**
    * @brief EC m_join Model
    * @file /application/models/m_join.php
	* @author con100 <con100@playplus.net>
	* @since 18/04/2013
	* @copyright Copyright (c) 2013, PlayPlus
    **/
    class M_join extends CI_Model {

        function __construct()
        {
            parent::__construct();
        }

		function join_check_id($uid)
		{
			$sql = "select id from ebs_member where id=?";			
            $data=$this->db->query($sql, array($uid));
			
			if($data->num_rows()>0){
				return $result = array("Result"=>FALSE);
			}
			else{
				return $result = array("Result"=>TRUE);
			}
		}

		function join_check_nick($nick)
		{
			$sql = "select name from ebs_member where name=?";
			$data=$this->db->query($sql, array($nick));

			if($data->num_rows()>0){
				return $result = array("Result"=>FALSE);
			}
			else{
				return $result = array("Result"=>TRUE);
			}
		}
		
		function join_check_refer($uid, $refer)
		{
			$time = time();
			$sql = "select id, my_refer from ebs_member where my_refer=?";
			$data=$this->db->query($sql, array($refer));
			
			if ($data->num_rows()>0) {
				$refer_data=$data->row_array();
				$this->db->trans_start();
				$sql = "update ebs_member set fr_refer = ? where id = ?";
				$data = $this->db->query($sql, array($refer,$uid));
				//$sql1 = "insert into ebs_refer (id, refer_id, refer_time, refer_reward) values(?,?,?,?)";
				//$data1 = $this->db->query($sql1, array($uid,$refer_data['id'],$time,null));
				$this->db->trans_complete();

				$result = array("Result"=>TRUE);
			}
			else{
				$result = array("Result"=>FALSE, "Result_msg"=>"No Refer Code");
			}
		}

		function join_check($uid, $upw, $nick)
		{
			if($uid && $upw && $nick){
				$refer_code = $this->eclib->joinrefercode();
				$time = time();
				$this->db->trans_start();
				$sql = "insert into ebs_member(id,pw,name,timestemp_moon, my_refer) values (?,PASSWORD(?),?,?,?)";
				$data = $this->db->query($sql, array($uid,$upw,$nick,$time,$refer_code));
				$this->db->trans_complete();
				$result = array("Result"=>TRUE);
			}
			else{
				$result = array("Result"=>FALSE);
			}
		}

		function login($uid, $upw)
		{			
			$sql = "select no from ebs_member where id=? and pw=PASSWORD(?)";
			$data=$this->db->query($sql, array($uid,$upw));

			if($data->num_rows()>0){
				$user_data = $data->row_array();
				$time = time();
				$this->db->trans_start();
				$sql = "update ebs_member set login = '1', timestemp_active = ? where no=?";
				$data=$this->db->query($sql, array($time,$user_data['no']));
				$this->db->trans_complete();
				$result = array("Result"=>TRUE);
			}
			else{
				$result = array("Result"=>FALSE);
			}
		}
	}